﻿
namespace Triad.Controls
{
    using System;
    using System.Web.UI;
    using System.Web.UI.Design;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Image = System.Web.UI.WebControls.Image;
    using System.Configuration;
    using System.IO;

    public partial class tfListView
    {
        private string ThemeDir;
        private string ControlDir;
        /// <summary>
        /// Gets the design time image.
        /// </summary>
        /// <param name="item">The img.</param>
        /// <returns></returns>
        private string GetDesignTimeUrl(string item)
        {
            var webApp = (IWebApplication)Site.GetService(typeof(IWebApplication));
            var result = webApp.GetProjectItemFromUrl(item);
            return result.PhysicalPath;
        }

        private void RenderDesignTimeCode(HtmlTextWriter output)
        {
            ThemeDir = "~/_fw/theme/red";
            if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["Theme"]))
            {
                ThemeDir = ConfigurationManager.AppSettings["Theme"];
            }
            ControlDir = "~/_fw/controls";
            if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["Triad.Controls"]))
            {
                ControlDir = ConfigurationManager.AppSettings["Triad.Controls"];
            }

            //var txt = new Label { Text = GetDesignTimeUrl(ThemeDir + "/tfLView.css") };
            //txt.RenderControl(output);
            output.WriteBeginTag("link");
            output.WriteAttribute("type", "text/css");
            output.WriteAttribute("rel", "stylesheet");
            output.Write(ReadCSS());
            output.WriteEndTag("link");

            HtmlLink cssLink = new HtmlLink {Href = GetDesignTimeUrl(ThemeDir + "/tfLView.css")};
            cssLink.Attributes.Add("rel", "stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            cssLink.RenderControl(output);
            
            //Panel cssdiv = new Panel();
            //cssdiv.Controls.Add(cssLink);
            //cssdiv.RenderControl(output);

            //output.WriteAttribute("rel", "stylesheet");
            //output.Write(ReadCSS());

            //output.WriteBeginTag("DIV");
            //output.WriteAttribute("CssClass","lvCon");
            //output.WriteBeginTag("DIV");
            //output.WriteAttribute("CssClass", "lvMain");

            if (AllowGroupBy) { GetGroupByTable().RenderControl(output); }
            if (AllowSearch) { GetSearchTable().RenderControl(output); }
            GetHeaderandRows().RenderControl(output);
            GetFooterTable().RenderControl(output);
            //output.WriteEndTag("DIV");
            //output.WriteEndTag("DIV");

        }

        private string ReadCSS()
        {
            TextReader fs = new StreamReader(GetDesignTimeUrl(ThemeDir + "/tfLView.css"));
            string t = fs.ReadToEnd();
            fs.Close();
            return t;
        }

        #region Design-Time Render
        private Table GetGroupByTable()
        {
            var groupByTable = new Table { Width = GridWidth, CssClass = "tfTable"};
            var groupByRow = new TableRow();
            var groupByCell = new TableCell
            {
                HorizontalAlign = HorizontalAlign.Left,
                ColumnSpan = 2,
                Text = "Drag Header here to Group..."
            };

            groupByRow.Cells.Add(groupByCell);
            groupByTable.Rows.Add(groupByRow);
            return groupByTable;
        }

        private Table GetSearchTable()
        {
            var searchTable = new Table { Width = GridWidth };

            var searchRow = new TableRow();
            var searchCell = new TableCell
            {
                HorizontalAlign = HorizontalAlign.Left,
                ColumnSpan = 2,
            };
            var searchImage = new Image
            {
                ImageUrl = GetDesignTimeUrl(ControlDir+"/images/sp_icoSearch.gif"),
                ImageAlign = ImageAlign.AbsMiddle
            };
            searchImage.Style.Add("cursor", "pointer");
            var searchTextbox = new TextBox { Width = new Unit(100) };
            var searchDropDownList = new DropDownList();
            searchDropDownList.Items.Add("All Fields");
            searchCell.Controls.Add(searchDropDownList);
            searchCell.Controls.Add(searchTextbox);
            searchCell.Controls.Add(searchImage);
            searchRow.Cells.Add(searchCell);
            searchTable.Rows.Add(searchRow);

            return searchTable;
        }

        private Table GetHeaderandRows()
        {

            var headerTable = new Table{Width = GridWidth};
            int nX;
            int nY;
            //headerTable.CssClass = "tfTable tfRow";
            var headerRow = new TableHeaderRow();
            for (nX = 0; nX < 7; nX++)
            {
                var hCell = new TableHeaderCell { Text = "Head " + nX };
                headerRow.Cells.Add(hCell);
            }

            headerTable.Rows.Add(headerRow);

            for (nY = 0; nY < 5; nY++)
            {
                var nRow = new TableRow();
                for (nX = 0; nX < 7; nX++)
                {
                    var tCell = new TableCell { Text = "R" + nY + "C" + nX };
                    nRow.Cells.Add(tCell);
                }
                headerTable.Rows.Add(nRow);
            }
            return headerTable;
        }

        private Table GetFooterTable()
        {
            var navigatorTable = new Table {Width = GridWidth, CssClass = "tfFooter"};
            var pagerRow = new TableRow();
            
            var pagerCell0 = new TableCell { Text = "&nbsp;", Width = new Unit("10") };
            pagerRow.Cells.Add(pagerCell0);

            var pagerCell1 = new TableCell { Width = new Unit("50") };
            if (AllowEdit) { pagerCell1.Controls.Add(new Button { Text = "Update" }); }
            pagerRow.Cells.Add(pagerCell1);

            var pagerCell2 = new TableCell { Width = new Unit("50") };
            if (AllowAdd)  { pagerCell2.Controls.Add(new Button { Text = "Add New" }); }
            pagerRow.Cells.Add(pagerCell2);

            var pagerCell3 = new TableCell();
            if (AllowDelete)  { pagerCell3.Controls.Add(new Button { Text = "Delete Selected" }); }
            pagerRow.Cells.Add(pagerCell3);

            var pagerCell35 = new TableCell();
            if (EnableExport) {  pagerCell35.Controls.Add(new ImageButton { ImageUrl = ControlDir+"/images/export.gif" });  }

            pagerRow.Cells.Add(pagerCell35);

            if (AllowPaging)
            {
                var pagerCell4 = new TableCell
                {
                    Text = "Items:&nbsp;&nbsp;",
                    Width = new Unit("90%"),
                    HorizontalAlign = HorizontalAlign.Right
                };
                pagerCell4.Controls.Add(new DropDownList { Items = { "ALL", "5", "10", "15", "20", "30", "50", "100" } });
                pagerCell4.Controls.Add(new Image
                {
                    ImageUrl = GetDesignTimeUrl(ControlDir+"/images/first.jpg"),
                    Width = new Unit("15")
                });
                pagerRow.Cells.Add(pagerCell4);

                var pagerCell5 = new TableCell();
                pagerCell5.Controls.Add(new Image
                {
                    ImageUrl = GetDesignTimeUrl(ControlDir+"/images/previous.jpg"),
                    Width = new Unit("15")
                });
                pagerRow.Cells.Add(pagerCell5);

                var pagerCell6 = new TableCell { Wrap = false };
                pagerCell6.Controls.Add(new TextBox { Width = new Unit("20") });
                pagerCell6.Controls.Add(new Label { Text = " / 1" });
                pagerRow.Cells.Add(pagerCell6);

                var pagerCell7 = new TableCell();
                pagerCell7.Controls.Add(new Image
                {
                    ImageUrl = GetDesignTimeUrl(ControlDir+"/images/next.jpg"),
                    Width = new Unit("15")
                });
                pagerRow.Cells.Add(pagerCell7);

                var pagerCell8 = new TableCell();
                pagerCell8.Controls.Add(new Image
                {
                    ImageUrl = GetDesignTimeUrl(ControlDir+"/images/last.jpg"),
                    Width = new Unit("15")
                });
                pagerRow.Cells.Add(pagerCell8);
            }

            navigatorTable.Rows.Add(pagerRow);
            return navigatorTable;
        }

        #endregion
    } //End Class
}
